<?php
namespace jy_jianghu;
defined('BY_JYA') or exit('error');
class member extends \plugin{
	public function checklogin($storeid='',$area_agentid='',$area_code=''){
		global $_AR;
		if(!$_SESSION['uid']){
			$_SESSION['forward_url']=$_AR['access_json'];
			model('core/core')->checklogin();
		}else{
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
			if(empty($member)){
				model('core/member')->logout();
				model('core/core')->checklogin('jy_jianghu/home/');
			}else{
				$user=pdo_get('jy_jianghu_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
				if(!$user['id']){
					$insert=array(
						'mid'=>$_SESSION['uid'],
						'uniacid'=>$_SESSION['uniacid'],
						'createtime'=>TIMESTAMP,
					);
					pdo_insert('jy_jianghu_member',$insert);
				}
			}
		}
	}
	public function setfromuser($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$fromuser=pdo_getcolumn('jy_jianghu_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'fromuser');
		if(!$fromuser && $_SESSION['fromuser'] && $_SESSION['fromuser'] !=$mid){
			pdo_update('jy_jianghu_member',array('fromuser'=>$_SESSION['fromuser']),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		}
		return false;
	}
	public function getdetail($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$user=pdo_get('jy_jianghu_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		return $user;
	}
	//加入帮派获得内力
	public function sendbang_neili($bangpai,$mid){
		if(!$bangpai['id']){
			return false;
		}
		$basic=$this->plugin_setting('basic');
		$myjias=pdo_getall('jy_jianghu_myjia',array('jianghu_id'=>$bangpai['id'],'uniacid'=>$_SESSION['uniacid']),array('mid'));
		$level=pdo_getcolumn('jy_weishop_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'level');
		//高级长老绑定等级
		if($basic['gaogroup_id'] && $basic['gaogroup_id']==$level){
			$neili=$basic['gaogroup_fee']?:1;
		}elseif($basic['zhonggroup_id'] && $basic['zhonggroup_id']==$level){
			$neili=$basic['zhonggroup_fee']?:1;
		}elseif($basic['digroup_id'] && $basic['digroup_id']==$level){
			$neili=$basic['digroup_fee']?:1;
		}else{
			$neili=$basic['group_fee']?:1;
		}
		foreach($myjias as $item){			
			$this->recharge($item['mid'],$neili,1,'新加入帮众',$bangpai['id']);
		}
	}
	//帮众消费
	public function get_orderpay($order){
		if(!$order['mid'] || $order['price']<=0){
			return false;
		}
		$bangpais=pdo_getall('jy_jianghu_myjia',array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),array('mid','jianghu_id'));
		if($bangpais){
			foreach($bangpais as $item){
				$bangpai=pdo_get('jy_jianghu_bangpai',array('id'=>$item['jianghu_id'],'uniacid'=>$_SESSION['uniacid']));
				$this->sendbangfee_neili($bangpai,$order['price']);
			}
			return true;
		}
		return false;
	}
	//消费获得内力
	public function sendbangfee_neili($bangpai,$fee){
		$basic=$this->plugin_setting('basic');
		$myjias=pdo_getall('jy_jianghu_myjia',array('jianghu_id'=>$bangpai['id'],'uniacid'=>$_SESSION['uniacid']),array('mid'));
		$basic['payfee']=$basic['payfee']?:1;
		$basic['payfee_li']=$basic['payfee_li']?:1;
		$neili=$basic['payfee_li']*$fee/$basic['payfee'];
		foreach($myjias as $item){		
			$this->recharge($item['mid'],$neili,1,'帮众消费',$bangpai['id']);
		}
	}
	//type 0消费，1获得
	public function recharge($mid=0,$fee=0,$type=0,$remark='',$jianghu_id=0){
		$mid=$mid?:$_SESSION['uid'];
		if(!intval($fee)){
			return false;
		}
		$member=pdo_get('jy_jianghu_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$update=array();
		if($type==0){
			if($member['neili']<$fee){
				exi('内力不够哦！','error');
			}
			$update['neili -=']=$fee;
			$msg='消耗内力:'.$fee;
			$type=0;
		}elseif($type==1){
			$update['neili +=']=$fee;
			$msg='获得内力:'.$fee;
			$type=1;
		}
		$user=pdo_update('jy_jianghu_member',$update,array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'jianghu_id'=>$jianghu_id,
			'msg'=>$remark.$msg,
			'fee'=>$fee,
			'isadd'=>$type,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_jianghu_neili_log',$insert);
		$params=array(
			'msg'=>$remark.$msg,
			'jianghu_id'=>$jianghu_id,
			'status'=>1,
			'mid'=>$mid,
		);
		$this->model('model')->set_log($params,$type);
		return true;
	}
}